package scau.online.web.user;

import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import scau.cty.base.common.BaseController;
import scau.cty.base.common.IBaseService;
import scau.cty.dictionary.Code;
import scau.cty.entity.Cart;
import scau.cty.entity.Goods;
import scau.cty.entity.User;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Sunlight on 2017/4/18.
 */
@Api(value = "购物车相关",description = "购物车相关接口")
@RequestMapping(value = "/user/cart",produces = "application/json")
@RestController
public class CartController extends BaseController{


    @Autowired
    private IBaseService<Cart> cartIBaseService;
    @Autowired
    private IBaseService<User> userIBaseService;
    @Autowired
    private IBaseService<Goods> goodsIBaseService;


    /**
     * 获取登陆用户的购物车列表
     * 其中cartList 和 goodsList 是一一对应的关系
     * @return
     */
    @ApiOperation(value = "获取登陆用户的购物车列表")
    @GetMapping("/list")
    public String getUserCartList(){
        Map map = new LinkedHashMap<>();
        Subject subject = SecurityUtils.getSubject();
        String username = (String) subject.getPrincipal();
        User user = userIBaseService.findSelectPropertyWhereAnd("username",username,"userId").get(0);
        List<Cart> cartList = cartIBaseService.findSelectPropertyWhereAnd("userId",user.getId());
        List<Goods> goodsList = new ArrayList<>();
        if(cartList.size() != 0){
            for (Cart c:
                 cartList) {
                goodsList.add(goodsIBaseService.findByKey(c.getGoodsId()));
            }
        }
        sendCode(map, Code.OPERATION_SUCCESS);
        map.put("cartList",cartList);
        map.put("goodsList",goodsList);
        return JSON.toJSONString(map);
    }

    /**
     * 加入商品进入购物车接口
     * 要先判断是否已存在
     * @param goodsId
     * @return
     */
    @ApiOperation(value = "加入商品进入购物车接口")
    @ApiImplicitParam(name = "goodsId",value = "要加入购物完成的商品唯一id",required = true,dataType = "String",paramType = "query")
    @PostMapping("/add")
    public String insertGoodsCart(String goodsId){
        Map map = new LinkedHashMap();
        if(goodsId == null || goodsId == ""){
            sendCode(map,Code.ILLEGAL_DATA_ERROR);
            return JSON.toJSONString(map);
        }
        //先检查 该用户的购物车中是否已存在该商品  是则 counts++ 即可 否再新增
        Subject subject = SecurityUtils.getSubject();
        String username = (String) subject.getPrincipal();
        User user = userIBaseService.findSelectPropertyWhereAnd("username",username,"userId").get(0);
        List<String> proList = new ArrayList<>();
        List valueList = new ArrayList();
        proList.add("userId");
        proList.add("goodsId");
        valueList.add(user.getId());
        valueList.add(goodsId);
        List<Cart> cartList = cartIBaseService.findSelectPropertyWhereAnd(proList, valueList, "id", "goodsCount");
        if(cartList.size() != 0){ //已存在
            Cart cart = cartList.get(0);
            Integer goodsCount = cart.getGoodsCount();
            cart.setGoodsCount(++goodsCount);
            cartIBaseService.updateNotNull(cart);
            sendCode(map,Code.OPERATION_SUCCESS);
            return JSON.toJSONString(map);
        }else{ //新增
            Cart cart = new Cart();
            cart.setUserId(user.getId());
            cart.setGoodsId(goodsId);
            cart.setGoodsCount(1); //默认为1
            cartIBaseService.add(cart);
            sendCode(map,Code.OPERATION_SUCCESS);
            return JSON.toJSONString(map);
        }
    }

    /**
     * 根据购物车的唯一标识删除购物车的商品(非goodsId)
     * @param id
     * @return
     */
    @ApiOperation(value = "删除购物车商品接口")
    @ApiImplicitParam(name = "id",value = "要删除的购物车内的商品唯一id(非goodsId)",required = true,dataType = "String",paramType = "query")
    @PostMapping("/remove")
    public String removeCartGoods(String id){
        Map map = new LinkedHashMap();
        if(id == null || id == ""){
            sendCode(map,Code.ILLEGAL_DATA_ERROR);
            return JSON.toJSONString(map);
        }
        cartIBaseService.delete(id);
        sendCode(map,Code.OPERATION_SUCCESS);
        return JSON.toJSONString(map);
    }


}
